package com.shuai;

import com.shuai.api.UserService;
import com.shuai.api.impl.UserServiceImpl;
import com.shuai.common.ServiceMetaInfo;
import com.shuai.config.RegistryConfig;
import com.shuai.config.RpcConfig;
import com.shuai.registry.LocalRegistry;
import com.shuai.registry.Registry;
import com.shuai.registry.RegistryFactory;
import com.shuai.server.HttpServer;
import com.shuai.server.VertxHttpServer;

/** 服务提供者示例 > */
public class ProviderExample {

  public static void main(String[] args) {
    // RPC 框架初始化
    RpcApplication.init();

    // 注册服务
    String serviceName = UserService.class.getName();
    LocalRegistry.register(serviceName, UserServiceImpl.class);

    // 注册服务到注册中心
    RpcConfig rpcConfig = RpcApplication.getRpcConfig();
    RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
    Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());
    ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
    serviceMetaInfo.setServiceName(serviceName);
    serviceMetaInfo.setServiceHost(rpcConfig.getServerHost());
    serviceMetaInfo.setServicePort(rpcConfig.getServerPort());
    try {
      registry.register(serviceMetaInfo);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    // 启动 web 服务
    HttpServer httpServer = new VertxHttpServer();
    httpServer.doStart(RpcApplication.getRpcConfig().getServerPort());
  }
}
